package fields;

import java.io.RandomAccessFile;

import data.AbstractDataType;
import data.EmailEntry;
import data.StringType;
import fileProcessing.ParsingException;

/**
 * field for username,password,email,security questions and answers
 * @author Andrew
 *
 */
public class AccountInfo extends AbstractField {
	
	public AccountInfo(String fieldName,String category) {
		super(fieldName,category);
	}

	@Override
	public void addData(RandomAccessFile dataFile, String value) throws ParsingException{
		new StringType(value).writeValue(dataFile);		
	}
	
	public AbstractDataType getValue(RandomAccessFile dataFile) throws ParsingException{
		return new StringType(dataFile);
	}

	@Override
	public int verify(String s) {
		int result = 0;
		
		if(this.name.equalsIgnoreCase("username")){
			if(s.contains(" "))
				result = 0;
			else 
				result = 2;
		}
		else if(this.name.equalsIgnoreCase("password")){
			if(s.contains(" "))
				result = 0;
			else 
				result = 2;
		}
		else if(this.name.equalsIgnoreCase("email")){
			try {
				if(new EmailEntry().isValid(s))
					result = 1;
				else
					result = 0;
			} catch (ParsingException e) {
				// ignore
			}
			
		}
		else if(this.name.equalsIgnoreCase("security question")){
			result = 2;
		}
		else if(this.name.equalsIgnoreCase("security question answer")){
			result = 2;
		}
		return result;
	}
}
